package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.domain.MessageBoard;
import cn.wolfcode.ssm.service.IMessageBoardService;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "message/list";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id){
        MessageBoard messageBoard = messageBoardService.selectDetailById(id);
        model.addAttribute("messageBoard", messageBoard);
        return "message/detail";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            messageBoardService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(MessageBoard messageBoard){
        messageBoardService.save(messageBoard);
        return new JsonResult();
    }
}
